import CryptoJS from 'crypto-js'
const CryptoSecret = 'c83c7138b57d7dbabcd3c38dc37445f4'
const secret = 'c83c7138b57d7dbabcd3c38dc37445f4'

export function encrypt(data) {
  const str = data
  return CryptoJS.AES.encrypt(str, CryptoSecret).toString()
}

export function decrypt(data) {
  const bytes = CryptoJS.AES.decrypt(data, CryptoSecret)
  const str = bytes.toString(CryptoJS.enc.Utf8)
  return str || ''
}

/**
 * ### 字符串加密
 * @param text 需要加密的文本
 * @returns 加密后的文本
 */
export function encryptWeb(text) {
  const key = CryptoJS.enc.Utf8.parse(secret)
  const srcs = CryptoJS.enc.Utf8.parse(text)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
  return encrypted.toString()
}

/**
 * ### 字符串解密
 * @param text 需要解密的文本
 * @returns 解密后的文本
 */
export function decryptWeb(word) {
  const key = CryptoJS.enc.Utf8.parse(secret)
  const decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
  return CryptoJS.enc.Utf8.stringify(decrypt).toString()
}
